const http=require("http");
const fs = require("fs");
const path = require("path");
var file = process.argv[2];

http.createServer((req,res)=>{
    if(typeof file == 'undefined'){
        // var filePath = path.join(__dirname,"/fileReader3.js");

        var readable = fs.createReadStream(path.join(__dirname,"/fileReader3.js"));
        readable.on("data", function(chunk){
            console.log(chunk.toString("utf8"));
            res.end(chunk.toString("utf8"));
        })
        readable.on("end",function(){
            console.log("###");
        })
    }
    else {
        fs.access(path.join(__dirname,"/"+file),(err)=>{
            if(err){ console.log("该文件不存在"); }
            else{
                readable.on("data", function(chunk){
                    res.end(chunk.toString("utf8"));
                })
                readable.on("end",function(){
                    console.log("###");
                })
            }
        })
    }

}).listen(8081);
console.log("server is listening 8081");